<template>
  <div class="merchant-login-page">
    <div class="login-container">
      <div class="left-panel">
        <img src="https://img.freepik.com/free-vector/cosmetic-items-beauty-saloon-tools-women-makeup_107791-13840.jpg" alt="美妆商城" class="side-image">
        <div class="overlay">
          <div class="promo-text">
            <h2>美妆商城商家中心</h2>
            <p>在这里开始您的美妆事业</p>
          </div>
        </div>
      </div>
      
      <div class="right-panel">
        <div class="logo-container">
          <img src="https://cdn-icons-png.flaticon.com/512/7612/7612176.png" alt="商家Logo" class="logo">
          <span class="logo-text">商家中心</span>
        </div>
        
        <div class="form-header">
          <h1>商家登录</h1>
          <p>请输入您的账号和密码</p>
        </div>
        
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="formRef"
          class="login-form"
          @keyup.enter="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              size="large"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              type="password"
              show-password
              size="large"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <div class="form-options">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <a href="javascript:void(0)" class="forgot-password">忘记密码?</a>
          </div>
          
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="submit-button"
            size="large"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
          
          <div class="form-footer">
            <p>没有商家账号? <router-link to="/merchant/register" class="signup-link">立即注册</router-link></p>
            <p class="user-link"><router-link to="/login">用户登录</router-link></p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../../stores/user';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';

// 表单数据和验证
const loginForm = reactive({
  username: '',
  password: ''
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
};

const formRef = ref(null);
const rememberMe = ref(false);

// 获取全局状态和路由
const userStore = useUserStore();
const router = useRouter();

// 从store中获取loading状态
const loading = computed(() => userStore.loading);

// 登录处理
const handleLogin = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        const success = await userStore.login({
          username: loginForm.username,
          password: loginForm.password,
          isMerchantLogin: true
        });
        
        if (success) {
          ElMessage.success('登录成功！');
          router.push('/merchant/dashboard');
        } else {
          ElMessage.error(userStore.error || '登录失败，请检查用户名和密码');
        }
      } catch (error) {
        console.error('登录错误:', error);
        ElMessage.error(error.message || '登录过程中发生错误');
      }
    }
  });
};
</script>

<style scoped>
.merchant-login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8f9fa;
  padding: 20px;
}

.login-container {
  display: flex;
  width: 1000px;
  height: 600px;
  background: white;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
}

.left-panel {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.side-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(rgba(50, 150, 50, 0.7), rgba(30, 130, 76, 0.9));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  text-align: center;
  padding: 0 40px;
}

.promo-text h2 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
}

.promo-text p {
  font-size: 18px;
  opacity: 0.9;
}

.right-panel {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.logo-container {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.logo {
  width: 40px;
  height: 40px;
}

.logo-text {
  font-size: 24px;
  font-weight: 600;
  margin-left: 12px;
  color: #34495e;
}

.form-header {
  margin-bottom: 30px;
}

.form-header h1 {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 8px;
}

.form-header p {
  color: #7f8c8d;
  font-size: 16px;
}

.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.forgot-password {
  color: #2ecc71;
  text-decoration: none;
  font-size: 14px;
}

.forgot-password:hover {
  text-decoration: underline;
}

.submit-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  border-radius: 5px;
  background-color: #2ecc71;
  border-color: #2ecc71;
  margin-bottom: 20px;
}

.submit-button:hover {
  background-color: #27ae60;
  border-color: #27ae60;
}

.form-footer {
  text-align: center;
  margin-top: auto;
  color: #7f8c8d;
}

.form-footer p {
  margin: 8px 0;
  font-size: 14px;
}

.signup-link, .user-link a {
  color: #2ecc71;
  font-weight: 600;
  text-decoration: none;
}

.signup-link:hover, .user-link a:hover {
  text-decoration: underline;
}

@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
    width: 100%;
    max-width: 500px;
    height: auto;

  }
  
  .left-panel {
    height: 200px;
  }
  
  .right-panel {
    padding: 30px 20px;
  }

}

@media (max-width: 576px) {
  .login-container {
    box-shadow: none;
    background: transparent;
  }
  
  .left-panel {
    display: none;
  }
  
  .right-panel {
    background: white;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  }
}
</style> 
